package de.tapps.brainstorming;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import de.tapps.brainstorming.R;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;

public class MainActivity extends SherlockListActivity implements AdListener, OnClickListener, OnItemSelectedListener, OnItemLongClickListener {
	public static final String data = "DATA";
	public InterstitialAd interstitial;
	public String[] topiclist;
	public String[] answerlist;
	public Button startnewtopic;
	public Button newanswer;
	public boolean isAdmin = false;
	public boolean is_topiclist = false;
	public boolean is_answerlist = false;
	public Integer[] topicIDs;
	public Integer[] answerIDs;
	public int currentTopicID;
	public int currentAnswerID;
	public String positionInProgram = "menu";
	public String order = "";
	public String[] topicDates;
	public String[] topicsWithDate;
	private GUI gui;
	public boolean closeit = false;

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			gui.backButton();
			return true;
			
		}
		else {
			return true;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
		setContentView(R.layout.activity_main);
		gui = new GUI(this);
		gui.generateMenu();
		interstitial = new InterstitialAd(this, "a15103aa00cc5d7");
		// Create ad request
		AdRequest adRequest = new AdRequest();
		// and begin loading your interstitial
		interstitial.loadAd(adRequest);
		SharedPreferences settings = getSharedPreferences(data, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("opened", settings.getInt("opened", 0) + 1);
		editor.commit();
		if (settings.getInt("opened", 0) >= 5) {
			interstitial.setAdListener(this);
			editor.putInt("opened", 0);
			editor.commit();
		}
		gui.registerListener();
		new IsAdmin(this).execute();
		new Enabled(this).execute();
		new AddUser(this).execute();
		new PhoneNumber(this).execute();
		updateCheck();
	}

	/**
	 * Gesture Detection
	 * 
	 * @param value
	 */
	@Override
	public void onClick(View v) {
		if (v == startnewtopic) {
			gui.onClickStartnewtopic();
			Log.e("Klick","Klick");
		}
		if (v == newanswer) {
			gui.onClickNewanswer();
		}
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String item = (String) getListAdapter().getItem(position);
		if (is_topiclist) {
			gui.position_chosenTopic(position);
		}
		else if (is_answerlist) {
			gui.positionIsAnswerlist(position);
		}
		if (item.equals(getResources().getString(R.string.credits))) {
			gui.positionCredits();
		}
		if (item.equals(getResources().getString(R.string.help))) {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=7QPYu78fhtY")));
			// do not add any extension
			// if your file is named sherif.mp4 and placed in /raw
			// use R.raw.sherif
		}
		if (item.equals(getResources().getString(R.string.settings))) {
			// showMessage(getResources().getString(R.string.functionDisabled));
		}
		if (item.equals(getResources().getString(R.string.language))) {
			gui.positionLanguage();
		}
		if (item.equals("Deutsch")) {
			gui.languageDE();
		}
		if (item.equals("English")) {
			gui.languageEN();
		}
		if (item.equals("Espa�ol")) {
			gui.languageES();
		}
		if (item.equals("Fran�ais")) {
			gui.languageFR();
		}
		if (item.equals("Italiano")) {
			gui.languageIT();
		}
		if (item.equals(getResources().getString(R.string.newanswer))) {
			gui.positionNewAnswers();
		}
		if (item.equals(getResources().getString(R.string.newtopic))) {
			gui.positionNewTopics();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		String shareBody = "Empfehlung: Android App BrainStorming Online.\n"
				+ "Betreiben Sie Online BrainStorming und werden Sie Teil der Community.\n" + "http://bit.ly/Z6hwjT";
		sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Empfehlung f�r Sie: BrainStorming Online");
		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
		startActivity(Intent.createChooser(sharingIntent, "BrainStorming Online teilen"));
		return true;
	}
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		// TODO Auto-generated method stub
		if (pos == 0) {
			// order = "newestactivity";
			order = "newestactivity";
			new TopicList(this).execute();
		}
		if (pos == 1) {
			order = "newest";
			new TopicList(this).execute();
		}
		if (pos == 2) {
			order = "oldest";
			new TopicList(this).execute();
		}
		if (pos == 3) {
			order = "mostanswers";
			new TopicList(this).execute();
		}
		if (pos == 4) {
			order = "fewestanswers";
			new TopicList(this).execute();
		}
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onDismissScreen(Ad arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onLeaveApplication(Ad arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onPresentScreen(Ad arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onReceiveAd(Ad arg0) {
		if (interstitial.isReady()) {
			interstitial.show();
		}
		// if (interstitial.isReady()) {

		// interstitial.show();

		// } else {
		// showMessage("Not ready");
		// }
	}
	public void updateCheck(){
		new UpdateCheck(this).execute();
	}
	public void showNotification(String text){
		 final int ID_NOT1 = 0;
   
   	NotificationManager notMan;
   	notMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


   	Notification notification = new Notification(R.drawable.statusbar, text,System.currentTimeMillis());
   	
   	notification.flags = Notification.FLAG_AUTO_CANCEL;
   	
   	PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
   	new Intent(),
   	PendingIntent.FLAG_UPDATE_CURRENT);
   	notification.setLatestEventInfo(this, "Online-Brainstorming", text, contentIntent);


   	notMan.notify(ID_NOT1, notification);
	}
	@Override
	public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		if (is_topiclist){
			gui.reportAnswer(position);
		}
		
		
		gui.isAdmin();
		return true;

		    //do your stuff here 
	  
		
	}
}
